﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;
public partial class Admagazine_TAdMagazine : System.Web.UI.Page
{
    public int pagesize = 20;
    public string pager = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //TUser1.CheckUser(this);
            Bind();
            if (Request["opration"] != null)
                switch (Request["opration"].ToString())
                {
                    case "bind":
                        BindInput();
                        break;
                    case "edit":
                        Save("edit");
                        break;
                    case "save":
                        Save("save");
                        break;
                    case "del":
                        Del();
                        break;
                    case "seltype":
                        GetTypes();
                        break;
                }
        }


    }
    public void GetTypes()
    {
        string sSql = "select infoid,infoname from t_ad_setup_info2 where typeid='S07'";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        string types = "";
        while (myReader.FDataReader.Read())
        {
            types += myReader.FDataReader["infoid"].ToString() + "|" + myReader.FDataReader["infoname"].ToString() + ",";
        }
        types = types.Remove(types.LastIndexOf(','));
        Response.Clear();
        Response.Write(types);
        Response.Flush();
        Response.Close();

    }

    public void Bind()
    {
        string sSql = "";
        int page = 1;
        if (Request["page"] != null)
        {
            page = int.Parse(Request["page"].ToString());
            sSql = "with a as ( select (select count(*) from t_ad_magazine) as countnum, row_number() over(order by input_time desc) as num,b.*,c.infoname from t_ad_magazine as b left join t_ad_setup_info2 as c on c.infoid=b.magtype ) select  * from a where num between " + ((page - 1) * pagesize + 1) + " and " + ((page - 1) * pagesize + pagesize);
        }
        else
        {
            sSql = "with a as ( select (select count(*) from t_ad_magazine) as countnum, row_number() over(order by input_time desc) as num,b.*,c.infoname from t_ad_magazine as b left join t_ad_setup_info2 as c on c.infoid=b.magtype ) select * from a where num between 1 and " + pagesize;
        }
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        dlTable.DataSource = myReader.FDataReader;
        dlTable.DataBind();
        if (myReader.FDataReader.HasRows)
        {
            int countnum = TSqlDB.ExecuteScalar(sSql);
            if (countnum % pagesize > 0)
                pager = GenPagebar(page, countnum / pagesize + 1);
            else
                pager = GenPagebar(page, countnum / pagesize);
        }
       
    }

    public void Save(string type)
    {

        TAdMagazine oMag = new TAdMagazine(Request["code"].ToString());
        oMag.Select();
        oMag.MagName = Request["name"].ToString();
        oMag.MagName2 = Request["name2"].ToString();
        oMag.MagType = Request["type"].ToString();
        oMag.CreateTime = Request["createtime"].ToString();
        oMag.Notice = Request["notice"].ToString();
        if (type == "edit")
        {
            oMag.Update();
        }
        else
        {
            if (oMag.MagCode == null)
            {
                oMag.MagCode = Request["code"].ToString();
                oMag.Input_Time = DateTime.Now;
                
                oMag.Insert();
            }
            else
            {
                Response.Clear();
                Response.Write("代码重复");
                Response.Flush();
                Response.Close();
                Bind();
                return;
            }
        }
        Response.Clear();
        Response.Flush();
        Response.Close();
        //Bind();



    }
    public void BindInput()
    {
        if (Request["id"] != null)
        {
            TAdMagazine oMag = new TAdMagazine(Request["id"].ToString());
            oMag.Select();
            Response.Clear();
            Response.Write(oMag.MagCode + "," + oMag.MagName + "," + oMag.MagName2 + "," + oMag.Notice + "," + oMag.MagType + "," + oMag.CreateTime);
            Response.Flush();
            Response.Close();
        }
    }
    public void Del()
    {
        if (Request["id"] != null)
        {
            TAdMagazine oMag = new TAdMagazine(Request["id"].ToString());
            oMag.Delete();
        }
        Bind();

    }


    protected void dlTable_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            TAdSetupInfo1 oASI1 = new TAdSetupInfo1(e.CommandArgument.ToString());
            TSqlDB.ExecuteNonQuery("delete from t_ad_magazine where magcode='" + e.CommandArgument.ToString() + "'");
            oASI1.Delete();

        }
        Bind();
    }


    public string GenPagebar(int PageNum, int PageCounts)
    {
        string result = "<div class=\"pagebox\">";


        if (PageCounts > 1)
        {
            if (PageNum == 1)//第一页
            {
                result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='TAdMagazine.aspx?opration=page&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
                result += "<span class=\"pagebox_next\"><a href='TAdMagazine.aspx?opration=page&page=2'\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a href='TAdMagazine.aspx?opration=page&page=" + PageCounts.ToString() + "'\">尾页</a></span>";
            }
            if ((PageNum > 1) && (PageNum < PageCounts))
            {
                result += "<span class=\"pagebox_num\"><a href='TAdMagazine.aspx?opration=page&page=1' \">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='TAdMagazine.aspx?opration=page&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a href='TAdMagazine.aspx?opration=page&page=" + (PageNum - 1).ToString() + "'\">上一页</a></span>";
                result += "<span class=\"pagebox_next\"><a href='TAdMagazine.aspx?opration=page&page=" + (PageNum + 1).ToString() + "'\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a href='TAdMagazine.aspx?opration=page&page=" + PageCounts.ToString() + "'\">尾页</a></span>";
            }

            if (PageNum == PageCounts)//最后一页
            {
                result += "<span class=\"pagebox_num\"><a href='TAdMagazine.aspx?opration=page&page=1'\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='TAdMagazine.aspx?opration=page&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a href='TAdMagazine.aspx?opration=page&page=" + (PageCounts - 1).ToString() + "'\">上一页</a></span>";
                result += "<span class=\"pagebox_next_nolink\">下一页</span>";
                result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
            }
        }
        else
        {
            result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
            result += "<span class=\"pagebox_num_nonce\"><a>1</a></span>";
            result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
            result += "<span class=\"pagebox_next_nolink\">下一页</span>";
            result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
        }

        result += "<span style=\"padding-top: 4px;\">&nbsp;(每页显示" + pagesize.ToString() + "行)</span>";
        result += "</div>";

        return result;
    }
    protected void btnPanel2Cancel_Click(object sender, EventArgs e)
    {
        Bind();
    }
}
